/*
 * @Author: WangTao borderpanda@163.com
 * @Date: 2025-09-04 21:34:25
 * @LastEditors: WangTao borderpanda@163.com
 * @LastEditTime: 2025-09-04 22:07:02
 * @Description:
 */
import {
  WebMapTileServiceImageryProvider,
  ImageryLayer,
  Resource,
} from "joCesium";
import { Viewer } from "@jodvf/cesium-3d";

export const tk = "42f933fd4ccdd3df06f91e0f01390110";
export const offlineUrl = "http://222.213.20.68"; // 地图离线服务
export const cacheUrl = "http://222.213.20.68:3000"; // 地形服务地址
export const subdomains = [
  `${offlineUrl}:5000`,
  `${offlineUrl}:5001`,
  `${offlineUrl}:5002`,
  `${offlineUrl}:5003`,
  `${offlineUrl}:5004`,
  `${offlineUrl}:5005`,
  `${offlineUrl}:5006`,
  `${offlineUrl}:5007`,
];
export default class CesiumViewer {
  static options: Viewer["mapOptions"] = {
    infoBox: false,
    animation: false,
    baseLayerPicker: false,
    geocoder: false,
    homeButton: false,
    sceneModePicker: false,
    timeline: false,
    navigationHelpButton: false,
    fullscreenButton: false,
    location: false, // 位置信息展示
    navigation: false, // toolbar
    copyrightLogo: false,
  };
  static initViewer(containerId: string): Viewer {
    const viewer = new Viewer(containerId, CesiumViewer.options);
    // 关闭不必要的场景渲染
    viewer.scene.moon.show = false;
    viewer.scene.fog.enabled = false;
    viewer.scene.sun.show = false;
    viewer.scene.skyBox.show = false;
    viewer.shadows = false;
    viewer.scene.globe.enableLighting = false;

    // 地图离线服务
    viewer.scene.imageryLayers.removeAll();
    const isIOS = window.navigator.appVersion.includes("iPhone");
    const proxy = Proxy;
    const useOutUrl = true;
    // ios不走代理，会被拦截
    // @ts-ignore
    proxy.getURL = (resource: string) =>
      resource.replaceAll("http%3A%2F%2F", "http://").replaceAll("%3A", ":");
    const imageryProvider = new WebMapTileServiceImageryProvider({
      url:
        isIOS || useOutUrl
          ? `https://{s}.tianditu.gov.cn/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&layer=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=${tk}`
          : new Resource({
              url: `{s}/cacheServer/api.tdtmap.com/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=${tk}`,
              // @ts-ignore
              proxy,
            }),
      layer: "img_w",
      style: "default",
      format: "tiles",
      tileMatrixSetID: "GoogleMapsCompatible",
      subdomains:
        isIOS || useOutUrl
          ? ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"]
          : subdomains,
      minimumLevel: 0, //最小层级
      maximumLevel: 18, //最大层级
      // show: true
    });
    viewer.scene.imageryLayers.add(new ImageryLayer(imageryProvider, {}));
    // 隐藏cesium版权信息
    (viewer.cesiumWidget.creditContainer as HTMLElement).style.display = "none";
    viewer.resize();
    return viewer;
  }
}
